package fun.hijklmn.magic.admin.server.config;

import fun.hijklmn.magic.admin.server.endpoint.JwtAuthenticationEntryPoint;
import fun.hijklmn.magic.admin.server.filter.JWTAuthenticationFilter;
import fun.hijklmn.magic.admin.server.filter.JWTAuthorizationFilter;
import fun.hijklmn.magic.admin.server.handler.JwtAccessDeniedHandler;
import fun.hijklmn.magic.admin.server.handler.JwtHandler;
import fun.hijklmn.magic.admin.server.handler.JwtLogoutSuccessHandler;
import fun.hijklmn.magic.admin.server.properties.JwtProperties;
import fun.hijklmn.magic.swagger.constants.MagicSwaggerConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;


/**
 * @Description
 * @Author fs
 * @Date 2021/6/25 15:58
 * @Version 0.0.1-SNAPSHOT
 **/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtHandler jwtHandler;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    @Qualifier("userDetailsServiceImpl")
    private UserDetailsService userDetailsService;

    @Lazy
    @Autowired
    public JWTAuthorizationFilter jwtAuthorizationFilter;

    @Autowired
    private JwtAccessDeniedHandler jwtAccessDeniedHandler;

    @Autowired
    @Qualifier("authenticationProviderImpl")
    private AuthenticationProvider authenticationProvider;

    @Lazy
    @Autowired
    public JWTAuthenticationFilter jwtAuthenticationFilter;

    @Autowired
    private JwtLogoutSuccessHandler jwtLogoutSuccessHandler;

    @Autowired
    private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(MagicSwaggerConstant.ignoreUrls);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and().authorizeRequests()
                .antMatchers(jwtProperties.getIgnoreUrls())
                .permitAll()
                .antMatchers(HttpMethod.OPTIONS)
                .permitAll()
                .anyRequest()
                .authenticated().and().logout().logoutUrl("/user/logout").logoutSuccessHandler(jwtLogoutSuccessHandler);

        http.headers().cacheControl();

        http.addFilterBefore(jwtAuthorizationFilter, BasicAuthenticationFilter.class);

        http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

        http.authenticationProvider(authenticationProvider).exceptionHandling().accessDeniedHandler(jwtAccessDeniedHandler).authenticationEntryPoint(jwtAuthenticationEntryPoint);

    }

    @Bean
    public JWTAuthorizationFilter jwtAuthorizationFilter() throws Exception {
        return new JWTAuthorizationFilter(super.authenticationManager(), jwtProperties, jwtHandler);
    }

    @Bean
    public JWTAuthenticationFilter jwtAuthenticationFilter() throws Exception {
        return new JWTAuthenticationFilter(super.authenticationManager(), jwtProperties, jwtHandler);
    }

}
